package com.cap.dreamcircle.View;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Base.BaseFragment;
import com.cap.dreamcircle.Consts.Constants;
import com.cap.dreamcircle.Model.Bean.UserInfoBean;
import com.cap.dreamcircle.Model.IUserModel;
import com.cap.dreamcircle.Model.Impl.UserModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.GlideUtils;
import com.cap.dreamcircle.Utils.JPushUtils;
import com.cap.dreamcircle.Utils.KeyCounterUtils;
import com.cap.dreamcircle.Utils.PreferenceUtils;
import com.cap.dreamcircle.Utils.TransformUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import rx.Subscriber;
import rx.functions.Action0;

/**
 * Created by liucxu on 2017/6/22.
 */

public class FragmentSettings extends BaseFragment {
    @BindView(R.id.tv_exit)
    TextView tv_exit;
    @BindView(R.id.circle_personal_header)
    CircleImageView circle_personal_header;
    @BindView(R.id.tv_user_name)
    TextView tv_user_name;
    @BindView(R.id.tv_my_topic)
    TextView tv_my_topic;
    @BindView(R.id.imv_settings)
    ImageView imv_settings;
    @BindView(R.id.layout_stars)
    RelativeLayout layout_stars;
    @BindView(R.id.layout_fans)
    RelativeLayout layout_fans;
    @BindView(R.id.tv_forbidden_list)
    TextView tv_forbidden_list;
    @BindView(R.id.layout_bind_phone)
    LinearLayout layout_bind_phone;
    @BindView(R.id.layout_bind_wechat)
    LinearLayout layout_bind_wechat;
    @BindView(R.id.tv_setting_phone_num)
    TextView tv_setting_phone_num;
    @BindView(R.id.tv_setting_wechat_nickname)
    TextView tv_setting_wechat_nickname;
    @BindView(R.id.layout_notify)
    RelativeLayout layout_notify;
    @BindView(R.id.imv_notify)
    ImageView imv_notify;
    private IUserModel userModel;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_settings;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this, mContextView);
        tv_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaterialDialog dialog = new MaterialDialog.Builder(getActivity()).title("提示").content("是否确定退出?").positiveText("确定").negativeText("取消").cancelable(false).onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        JPushUtils.UnRegisterJpush();
                        PreferenceUtils.SaveUserToken("");
                        PreferenceUtils.SaveUserId("");
                        Intent intent = new Intent(getActivity(), DeviceRegisterActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        App.GetInstance().finishActivity(getActivity());
                    }
                }).build();
                dialog.show();
            }
        });
        tv_my_topic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyCounterUtils.SendKeyCounter("f331fb1afb58417abd3d79b954f7f55d");
                Intent intent = new Intent(getContext(), PersonalInfoActivity.class);
                intent.putExtra("userId", PreferenceUtils.GetUserId());
                startActivity(intent);
            }
        });

        imv_settings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyCounterUtils.SendKeyCounter("84b2a11141344796b16a0a9ca85ef029");
                Intent intent = new Intent(getContext(), CompleteUserInfoActivity.class);
                intent.putExtra("modify_user_info", true);
                startActivity(intent);
            }
        });

        //我关注的人列表
        layout_stars.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), FellowFansActivity.class);
                intent.putExtra("KEY_IS_FELLOW", 0);
                getActivity().startActivity(intent);
            }
        });

        //我的粉丝
        layout_fans.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), FellowFansActivity.class);
                intent.putExtra("KEY_IS_FELLOW", 1);
                getActivity().startActivity(intent);
            }
        });

        //黑名单
        tv_forbidden_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), FellowFansActivity.class);
                intent.putExtra("KEY_IS_FELLOW", 2);
                getActivity().startActivity(intent);
            }
        });
        //通知
        layout_notify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MessageCenterActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void setupData() {
        userModel = new UserModelImpl();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            requestUserInfo();
        }
    }

    private void requestUserInfo() {
        userModel.RequestUserInfo().compose(TransformUtils.<UserInfoBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showProgress();
            }
        }).subscribe(new Subscriber<UserInfoBean>() {
            @Override
            public void onCompleted() {
                hideProgress();
            }

            @Override
            public void onError(Throwable e) {
                hideProgress();
            }

            @Override
            public void onNext(final UserInfoBean resposne) {
                hideProgress();
                if (resposne.getCode() == 0) {
                    GlideUtils.getInstance().LoadContextBitmap(getActivity(), Constants.BASE_URL + resposne.getHeadImgUrl(), circle_personal_header, R.mipmap.default_header, R.mipmap.default_header, GlideUtils.LOAD_BITMAP);
                    tv_user_name.setText(resposne.getNickName());
                    tv_exit.setVisibility(View.VISIBLE);
                    if ("mobile".equals(resposne.getLoginType())) {
                        layout_bind_wechat.setVisibility(View.VISIBLE);
                        layout_bind_phone.setVisibility(View.GONE);
                        tv_setting_phone_num.setText(resposne.getBdMobile());
                        tv_setting_wechat_nickname.setText(resposne.getBdWxUserName());
                        layout_bind_wechat.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(getActivity(), ChangeWechatActivity.class);
                                intent.putExtra("key_wechat_nick_name", resposne.getBdWxUserName());
                                intent.putExtra("key_UnionId", resposne.getBdWxUinonId());
                                startActivity(intent);
                            }
                        });
                    } else if ("wx".equals(resposne.getLoginType())) {
                        layout_bind_phone.setVisibility(View.VISIBLE);
                        layout_bind_wechat.setVisibility(View.GONE);
                        tv_setting_phone_num.setText(resposne.getBdMobile());
                        tv_setting_wechat_nickname.setText(resposne.getBdWxUserName());
                        //绑定手机
                        layout_bind_phone.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(getActivity(), ChangePhoneNumActivity.class);
                                intent.putExtra("key_phone_num", resposne.getBdMobile());
                                startActivity(intent);
                            }
                        });
                    } else if (TextUtils.isEmpty(resposne.getLoginType())) {
                        if ("mobile".equals(resposne.getUserType())) {
                            layout_bind_wechat.setVisibility(View.VISIBLE);
                            layout_bind_phone.setVisibility(View.GONE);
                            tv_setting_phone_num.setText(resposne.getBdMobile());
                            tv_setting_wechat_nickname.setText(resposne.getBdWxUserName());
                            layout_bind_wechat.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(getActivity(), ChangeWechatActivity.class);
                                    intent.putExtra("key_wechat_nick_name", resposne.getBdWxUserName());
                                    intent.putExtra("key_UnionId", resposne.getBdWxUinonId());
                                    startActivity(intent);
                                }
                            });
                        } else {
                            layout_bind_phone.setVisibility(View.VISIBLE);
                            layout_bind_wechat.setVisibility(View.GONE);
                            tv_setting_phone_num.setText(resposne.getBdMobile());
                            tv_setting_wechat_nickname.setText(resposne.getBdWxUserName());
                            //绑定手机
                            layout_bind_phone.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(getActivity(), ChangePhoneNumActivity.class);
                                    intent.putExtra("key_phone_num", resposne.getBdMobile());
                                    startActivity(intent);
                                }
                            });
                        }
                    }

                    if (resposne.getNewMsgCount() > 0) {
                        imv_notify.setVisibility(View.VISIBLE);
                    } else {
                        imv_notify.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        requestUserInfo();
    }
}
